package day02;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MySAXHanler extends DefaultHandler {

	/**
	 * uri是元素的命名空间，如果没有命名空间，那就是空字符串 
	 * localName 是那个schame 
	 * qNam e
	 * Attributes	  属性他们里面包含了若干的
	 */
//	public void startElement(String uri,
//											String localName, String qName,
//			Attributes attributes) throws SAXException {
//		StringBuffer sb = new StringBuffer();
//		sb.append("元素: " + localName + "\n");
//		sb.append("------属性: \n");
//		for (int i = 0; i < attributes.getLength(); i++) {
//			sb.append("      " + attributes.getLocalName(i) + " =");
//			sb.append(attributes.getValue(i) + "\n");
//		}
//		sb.append("-------属性列表结束-----\n");
//		System.out.println(sb.toString());
//	}

	public void startElement(String uri,String locagName, String qName ,Attributes attributes)throws SAXException{
		StringBuffer sb = new StringBuffer();
		sb.append("<" + qName);
		if(attributes.getLength() !=0){
			for(int i=0; i<attributes.getLength();i++){
				sb.append(" " + attributes.getLocalName(i) );
				sb.append(attributes.getValue(i));
			}
		}
		sb.append(">");
		System.out.print(sb.toString());
	}
	
	
	public void endElement(String uri, 
										  String localName, 
										  String qName,
			Attributes attributes) throws SAXException {
		
		System.out.println("<" + qName + ">");
	}
	/**
	 * char[]  里面存的是整个文档
	 * start 是从文档的开始的位子
	 * length是去多长
 	 * 
	 */
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String s = new String(ch,start,length).trim();
		
		if("".equals(s)){
		System.out.println(s);
		}
	}

	public void startDocument() throws SAXException {
		System.out.println("开始了");
	}

	public void endDocument() throws SAXException {
		System.out.println("结束了");
	}
}
